﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
    {
    public partial class GoodsDiv : Form
        {
        private DataSets.Dictionary.GoodsRow _row;
        public DataSets.Dictionary.GoodsRow Row
        {
        get
        {
        return _row;
            }
        set
            {
            if (value == null)
                throw new Util.DIException("Не задан разделяемый товар");
            _row = value;
            }
            }
        private DataSets.Dictionary.GoodsDataTable _goods;
        public DataSets.Dictionary.GoodsDataTable Goods
        {
        get
            {
            return _goods;
            }
        set
        { 
            if(value == null)
                throw new Util.DIException("Не передана таблица товаров!");
            _goods = value;
            }
            }
        public GoodsDiv(DataSets.Dictionary.GoodsRow row,DataSets.Dictionary.GoodsDataTable goods)
            {
            this.Row = row;
            this.Goods = goods;
            InitializeComponent();
            }

        private void btnCancel_Click(object sender, EventArgs e)
            {
            this.Close();
            }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            DataSets.DictionaryTableAdapters.GoodsTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.GoodsTableAdapter();
            Int32 repPack = Convert.ToInt32(textBox1.Text);
            DataSets.UtilTableAdapters.UtilQueries uAdapter = new DIProject.DataSets.UtilTableAdapters.UtilQueries();
            Int32 childGood = Int32.MinValue;
            object obj = uAdapter.GoodsChildQuery(Row.AssortmentId, Row.ManufacturerId, repPack);
            if ((!Convert.IsDBNull(obj)) && (obj != null))
            {
                childGood = (Int32)obj;
            }
            else
            {
                DataSets.Dictionary.GoodsRow r = this.Goods.NewGoodsRow();
                r.AssortmentId = Row.AssortmentId;
                r.ManufacturerId = Row.ManufacturerId;
                r.CountryId = Row.CountryId;
                // r.ChildId = Row.Id;
                r.AllowUnPack = false;
                r.RetailPack = Convert.ToInt32(textBox1.Text);
                r.WholesalePack = 1;
                if (!Row.IsManufactureBarCodeNull())
                    r.ManufactureBarCode = Row.ManufactureBarCode;
                if (!Row.IsFixedPriceNull())
                    r.FixedPrice = Row.FixedPrice;
                if (!string.IsNullOrEmpty(txbNote.Text))
                    r.Note = this.txbNote.Text;
                else
                    r.Note = null;
                r.MD = DateTime.Now;
                r.MW = FirstForm.Login;
                r.DefekturaAggregate = false;
                r.ToDelete = false;
                this.Goods.Rows.Add(r);                
                adapter.Update(this.Goods);
                this.Goods.AcceptChanges();
                DataSets.DictionaryTableAdapters.LastGodsTableAdapter adap = new DIProject.DataSets.DictionaryTableAdapters.LastGodsTableAdapter();
                DataSets.Dictionary.LastGodsDataTable tab = adap.GetData();
                DataSets.Dictionary.LastGodsRow lRow = tab.Rows[0] as DataSets.Dictionary.LastGodsRow;
                r.ManufactureName = lRow.ManufactureName;
                r.Name = lRow.Name;
                r.CountryName = lRow.CountryName;
                r.Original_Id = r.Id;
                r.Original_MD = r.MD;
                childGood = r.Id;
            }
            Row.ChildId = childGood;
            adapter.Update(this.Goods);
            this.Goods.AcceptChanges();
            this.Hide();
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
            {
            string str = textBox1.Text;
            if (string.IsNullOrEmpty(str))
            {
            MessageBox.Show("Необходимо ввести целое значение", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            textBox1.Focus();
            e.Cancel = true;
            return;
                }
            Int32 tmp;
            if (!Int32.TryParse(str, out tmp))
                {
                MessageBox.Show("Необходимо ввести целое значение", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Focus();
                e.Cancel = true;
                return;
                }
            }

        private void GoodsDiv_Load(object sender, EventArgs e)
            {
            lblDivGoods.Text = Row.Name;
            }
        }
    }